<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\Restore;
use App\Admin\Repositories\Combination;
use App\Admin\Server\DropdownMenu;
use App\Admin\Server\GroupService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CombinationController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     * 拼团管理
     */
    protected function grid()
    {
        return Grid::make(new Combination(['groupGoods','userRegimental']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('userRegimental.nickName','团长名称');
            $grid->column('groupGoods.0.image','拼团商品图片')->image(100,100);
            $grid->column('groupGoods.0.group_name','拼团商品名称');
            $grid->column('people','参团人数');
            $grid->column('groupGoods.0.group_number','成团人数');
            $grid->column('stop_time','成团时间');
            $grid->column('limit_quantity','限量');
            $grid->column('groupGoods.0.original_cost','原价');
            $grid->column('groupGoods.0.group_price','拼团价格');
            $grid->column('stop_time','拼团结束时间');
            $grid->column('created_at','拼团开始时间');
            $grid->column('state','拼团状态')->display(function ($e){
                if($e==1){
                    return '进行中';
                }elseif ($e==2){
                    return '已完成';
                }elseif ($e==3){
                    return '未完成';
                }
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
            $grid->filter(function ($filter) {
                // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
                $filter->scope('trashed', '回收站')->onlyTrashed();

            });
            //恢复数据
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    $actions->append(new Restore(\App\Models\Combination::class));
                }
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Combination(), function (Show $show) {
            $show->field('id')->sortable();
            //商品id
            $show->field('mer_id');
            //团长id
            $show->field('k_id');
            //商品图片
            $show->field('image')->image(100,100);

            //商品标题
            $show->field('title');
            //拼团人数
            $show->field('people');
            //拼团商品价格
            $show->field('price');
            //排序
            $show->field('sort');
            //销量
            $show->field('sales');
            //商品库存
            $show->field('stock');
            //添加时间
            $show->field('add_time');
            //商品状态
            $show->field('is_show');
            //商家名称
            $show->field('activity_state','活动状态');

            $show->field('start_time');
            $show->field('stop_time');
            $show->field('effective_time');
            $show->field('delete_time');

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Combination(), function (Form $form) {

            $form->display('id');
            //调用模态框进行添加商品
            $form->selectTable('goods_id')->rules('required')->from(GroupService::make())->saving(function ($e){
                return $e[0];
            });
            //商品图片使用七牛云进行登录
            $form->image('image')->uniqueName()->saveFullUrl();
            //商品标题
            $form->text('group_name','商品标题');
            $form->text('original_cost','商品原价');
            $form->text('group_price','拼团价格');
            //拼团人数
            $form->text('people','参团人数');
            //拼团商品价格
            $form->text('clustering_people','成团人数');
            //排序
            $form->radio('state','拼团状态')->options(['1'=>'开启','2'=>'关闭']);
            //销量
            $form->datetime('stop_time','结束时间');
            $form->datetime('open_time','开始时间');
            $form->text('effective_time','拼团有效期时间');
        });
    }
}
